package com.agriculture.monitoring.service;

import com.agriculture.monitoring.dto.DeviceDTO;
import com.agriculture.monitoring.entity.GeoDevice;
import com.agriculture.monitoring.repository.DeviceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class DeviceService {
    
    @Autowired
    private DeviceRepository deviceRepository;
    
    public List<DeviceDTO> getAllDevices() {
        return deviceRepository.findAll().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    public Optional<DeviceDTO> getDeviceById(Long id) {
        return deviceRepository.findById(id)
                .map(this::convertToDTO);
    }
    
    public Optional<DeviceDTO> getDeviceByCode(String deviceCode) {
        return deviceRepository.findByDeviceCode(deviceCode)
                .map(this::convertToDTO);
    }
    
    public List<DeviceDTO> getDevicesByStatus(String status) {
        return deviceRepository.findByStatus(status).stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    public DeviceDTO createDevice(DeviceDTO deviceDTO) {
        GeoDevice device = new GeoDevice();
        device.setDeviceCode(deviceDTO.getDeviceCode());
        device.setDeviceName(deviceDTO.getDeviceName());
        device.setDeviceType(deviceDTO.getDeviceType());
        device.setStatus("离线");
        
        GeoDevice saved = deviceRepository.save(device);
        return convertToDTO(saved);
    }
    
    public Optional<DeviceDTO> updateDevicePosition(Long deviceId, Double longitude, Double latitude) {
        Optional<GeoDevice> optionalDevice = deviceRepository.findById(deviceId);
        if (optionalDevice.isPresent()) {
            GeoDevice device = optionalDevice.get();
            device.setCurrentLongitude(longitude);
            device.setCurrentLatitude(latitude);
            device.setLastUpdateTime(LocalDateTime.now());
            device.setStatus("在线");
            
            GeoDevice updated = deviceRepository.save(device);
            return Optional.of(convertToDTO(updated));
        }
        return Optional.empty();
    }
    
    public boolean deleteDevice(Long id) {
        if (deviceRepository.existsById(id)) {
            deviceRepository.deleteById(id);
            return true;
        }
        return false;
    }
    
    private DeviceDTO convertToDTO(GeoDevice device) {
        DeviceDTO dto = new DeviceDTO();
        dto.setId(device.getId());
        dto.setDeviceCode(device.getDeviceCode());
        dto.setDeviceName(device.getDeviceName());
        dto.setDeviceType(device.getDeviceType());
        dto.setStatus(device.getStatus());
        dto.setLongitude(device.getCurrentLongitude());
        dto.setLatitude(device.getCurrentLatitude());
        dto.setLastUpdateTime(device.getLastUpdateTime());
        return dto;
    }
}